package com.swf.seed.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class FwsZip {
	private static int BUFFERSIZE = 4*1024;
	
	public FwsZip() {
	}
	public FwsZip(int bufferSize) {
		bufferSize = bufferSize;
		
	}
	
	
	
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		File dest = new File("D:/linux/key/abc.zip");
		File file = new File("D:/linux/key/abc/ecs.properties");
		//zipSingleFile(dest, file);
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
		zipFile(zos,file,null);
		zos.close();
	}
	
	public static void zipFile(ZipOutputStream zos,File file,String dir) throws IOException{
		if (file.isDirectory()){
			
		}else{
			String entryName = (dir!=null?(dir + "/"):"")+file.getName();
			System.out.println("add entry:"+entryName);
			ZipEntry entry = new ZipEntry(entryName);
			zos.putNextEntry(entry);
			BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
			byte[] buff = new byte[BUFFERSIZE];
			int len = 0;
			while ((len = is.read(buff))!=-1){
				zos.write(buff,0,len);
			}
			is.close();
		}
	}
	/**
	 * @param dest
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void zipSingleFile(File dest, File file)
			throws FileNotFoundException, IOException {
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));	
		ZipEntry entry = new ZipEntry(file.getName());
		zos.putNextEntry(entry);
		BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
		byte[] buff = new byte[4*1024];
		int len = 0;
		while ((len = is.read(buff))!=-1){
			zos.write(buff,0,len);
		}
		zos.flush();
		is.close();
		zos.close();
	}
	
}
